#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2022/9/14 17:27
# @Author  : CHENWang
# @Site    : 
# @File    : aave.py
# @Software: PyCharm

"""
脚本说明:
"""
import datetime
import json
import time
import pandas as pd
import requests
from dateutil.tz import tzutc
from quant_researcher.quant.datasource_fetch.crypto_api.defillama import get_detailed_tvl_by_protocol
from quant_researcher.quant.project_tool.time_tool import str_to_timestamp
from quant_researcher.quant.project_tool.wrapper_tools.common_wrappers import deco_retry


@deco_retry(retry=5, retry_sleep=15)
def get_aave_borrow_fee_rate(asset='USDC', network='v2-ethereum'):
    """
    从aave网站爬取借贷费率数据

    :param asset:
    :param network:
    :return:
    """
    if network == 'v2-ethereum':  # todo 目前aave全网锁仓量几乎都在v2-ethereum，后续如果转移了的话需要再修改
        asset_dict = {'BUSD': '0x4fabb145d64652a948d72533023f6e7a623c7c53',
                      'DAI': '0x6b175474e89094c44da98b954eedeac495271d0f',
                      'FEI': '0x956f47f50a910163d8bf957cf5846d573e7f87ca',
                      'USDC': '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
                      'USDT': '0xdac17f958d2ee523a2206206994597c13d831ec7',
                      'WBTC': '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
                      'ETH': '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'}
        appendix = '0xb53c1a33016b2dc2ff3653530bff1848a515c8c5'

    start = int(time.mktime(datetime.datetime(2020, 1, 1, 0, 0, 0, tzinfo=tzutc()).timetuple()))
    header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'}
    url = f'https://aave-api-v2.aave.com/data/rates-history?reserveId={asset_dict[asset]}{appendix}&from={start}&resolutionInHours=6'

    res = requests.get(url, headers=header, timeout=100)
    res = pd.DataFrame(json.loads(res.text))

    def time_format(i):
        year = i['year']
        month = str(i['month'] + 1).zfill(2)
        date = str(i['date']).zfill(2)
        hours = str(i['hours']).zfill(2)
        timestamp = str_to_timestamp(input_str=f"{year}-{month}-{date} {hours}:00", fmt='%Y-%m-%d %H:%M', tz_str='+0000')
        return timestamp

    res['timestamp'] = res['x'].apply(time_format)
    res['date'] = pd.to_datetime(res['timestamp'], unit='s').dt.tz_localize('UTC').dt.tz_convert('+0000')
    res['date'] = res['date'].dt.strftime('%Y-%m-%d')
    res['datetime'] = pd.to_datetime(res['timestamp'], unit='s').dt.tz_localize('UTC').dt.tz_convert('+0000')
    res['datetime'] = res['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')

    res.set_index('datetime', inplace=True)

    return res


def get_aave_borrow_amount():
    chainTvls_v = get_detailed_tvl_by_protocol(protocol_name='aave', tvl_type='chainTvls')
    chainTvls_v = chainTvls_v[~chainTvls_v.index.duplicated(keep='first')]  # index去重
    # chainTvls_v1 = get_detailed_tvl_by_protocol(protocol_name='aave-v1', tvl_type='chainTvls')
    # chainTvls_v2 = get_detailed_tvl_by_protocol(protocol_name='aave-v2', tvl_type='chainTvls')
    # chainTvls_v3 = get_detailed_tvl_by_protocol(protocol_name='aave-v3', tvl_type='chainTvls')
    # total_borrowed = pd.concat([chainTvls_v1['borrowed'], chainTvls_v2['borrowed'], chainTvls_v3['borrowed']], axis=1)
    # total_borrowed.fillna(0, inplace=True)
    # total_borrowed.sort_index(inplace=True)
    # total_borrowed['v1v2v3'] = total_borrowed.sum(axis=1)

    Tvls_v = get_detailed_tvl_by_protocol(protocol_name='aave', tvl_type='tvl')
    Tvls_v = Tvls_v[~Tvls_v.index.duplicated(keep='first')]  # index去重
    # Tvls_v1 = get_detailed_tvl_by_protocol(protocol_name='aave-v1', tvl_type='tvl')
    # Tvls_v2 = get_detailed_tvl_by_protocol(protocol_name='aave-v2', tvl_type='tvl')
    # Tvls_v3 = get_detailed_tvl_by_protocol(protocol_name='aave-v3', tvl_type='tvl')
    # total_locked = pd.concat([Tvls_v1['tvl'], Tvls_v2['tvl'], Tvls_v3['tvl']], axis=1)
    # total_locked.fillna(0, inplace=True)
    # total_locked.sort_index(inplace=True)
    # total_locked['v1v2v3'] = total_locked.sum(axis=1)

    total_utilization = chainTvls_v['borrowed'] / Tvls_v['tvl']
    total_utilization.name = 'total_utilization'

    tokensInUsd = get_detailed_tvl_by_protocol(protocol_name='aave', tvl_type='tokensInUsd')
    tokensInUsd = tokensInUsd[~tokensInUsd.index.duplicated(keep='first')]  # index去重
    borrow_amount_list = []
    # for asset in ['BUSD', 'DAI', 'FEI', 'USDC', 'USDT', 'WBTC', 'ETH', 'STETH']:
    #     borrow_fee_rate = get_aave_borrow_fee_rate(asset=asset, network='v2-ethereum')
    #     borrow_fee_rate = borrow_fee_rate.groupby(['date'])[borrow_fee_rate.columns[:4]].mean()
    #     borrow_amount = borrow_fee_rate['utilizationRate_avg'] * tokensInUsd[asset]
    #     borrow_amount.name = asset
    #     borrow_amount_list.append(borrow_amount)

    # import os
    # from quant_researcher.quant.project_tool.localize import DATA_DIR
    # for asset in ['DAI', 'USDC', 'WBTC', 'ETH']:
    #     file_path = os.path.join(DATA_DIR, f'margin_interest_rate')
    #     file_name = os.path.join(file_path, f'aave_{asset}_margin_interest_rate')
    #     borrow_fee_rate = pd.read_excel(f'{file_name}.xlsx', index_col='Unnamed: 0')
    #     if asset == 'ETH':
    #         borrow_amount = borrow_fee_rate['utilizationRate_avg'] * tokensInUsd['WETH']
    #     else:
    #         borrow_amount = borrow_fee_rate['utilizationRate_avg'] * tokensInUsd[asset]
    #     borrow_amount.name = asset
    #     borrow_amount_list.append(borrow_amount)
    # borrow_amount_df = pd.concat(borrow_amount_list, axis=1)
    # borrow_amount_df['total_borrowed'] = borrow_amount_df.sum(axis=1)

    borrow_amount_df = tokensInUsd[['DAI', 'USDC', 'WBTC', 'ETH']]

    total_df = pd.concat([borrow_amount_df, Tvls_v['tvl'], chainTvls_v['borrowed'], total_utilization], axis=1)

    return total_df


if __name__ == '__main__':
    df = get_aave_borrow_fee_rate(asset='USDC', network='v2-ethereum')

    df = get_aave_borrow_amount()
    from quant_researcher.quant.datasource_fetch.crypto_api.glassnode import get_prices
    import numpy as np
    import os
    from quant_researcher.quant.project_tool.localize import DATA_DIR
    price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2020-01-01', end_date=None)
    price_log_price['log_prices'] = np.log10(price_log_price['close'])
    all_frame = df.merge(price_log_price, left_index=True, right_index=True)
    all_frame.sort_index(inplace=True)
    file_path = os.path.join(DATA_DIR, f'margin_interest_rate')
    os.makedirs(file_path, exist_ok=True)
    file_name = os.path.join(file_path, f'aave_margin_tvl_borrowed')
    all_frame.to_excel(f'{file_name}.xlsx')